package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    /**
     * 新增套餐功能
     * @param setmealDTO
     * @return
     */
    @Override
    public void insertSetmealAndSetmealDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.saveSetmeal(setmeal);
        List<SetmealDish> setmealDish = setmealDTO.getSetmealDishes();
        if(setmealDish != null && setmealDish.size() > 0){
            for(SetmealDish dish : setmealDish) {
                dish.setSetmealId(setmeal.getId());
            }
            setMealDishMapper.saveSetmealDish(setmealDish);
        }

    }

    /**
     * 套餐分页查询功能
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 套餐删除功能
     * @param ids
     */
    public void deleteByIds(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if(setmeal.getStatus().equals(StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        for (Long id : ids) {
            setmealMapper.deleteById(id);
            setMealDishMapper.deleteBySetmealId(id);
        }
    }

    /**
     * 修改套餐和与套餐相关联的菜品
     * @param setmealDTO
     */

    public void updateSetmeal(SetmealDTO setmealDTO) {
        //修改套餐里的数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

        //删除和套餐相关的菜品联系表
        setMealDishMapper.deleteBySetmealId(setmealDTO.getId());
        //重新加入套餐相关的菜品联系表
        List<SetmealDish> setmealDish = setmealDTO.getSetmealDishes();
        if(setmealDish != null && setmealDish.size() > 0){
            for(SetmealDish dish : setmealDish) {
                dish.setSetmealId(setmeal.getId());
            }
            setMealDishMapper.saveSetmealDish(setmealDish);
        }
    }

    /**
     * 套餐启用或禁用
     * @param status
     */
    public void startOrStop(Integer status,Long id) {
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        List<Integer> setMealStatus = dishMapper.getStatus(id);
        for (Integer mealStatus : setMealStatus) {
            if(StatusConstant.DISABLE.equals(mealStatus)){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        setmealMapper.update(setmeal);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    public SetmealVO getByIdWithDish(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal = setmealMapper.getById(id);
        BeanUtils.copyProperties(setmeal,setmealVO);

        List<SetmealDish> setmealDishes = setMealDishMapper.getSetmealDishBySetmealId(id);

        setmealVO.setSetmealDishes(setmealDishes);
        String categoryName = setmealMapper.getCategoryName(id);
        setmealVO.setCategoryName(categoryName);

        return setmealVO;
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }



}
